<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采购订单编号" prop="sn" label-width="55">
        <el-input
          v-model="queryParams.sn"
          placeholder="请输入采购订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['mes:order:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['mes:order:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:order:export']"
        >导出</el-button>
      </el-col>
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="orderList" border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" align="center" prop="number" width="90"/>
      <el-table-column label="到货状态" align="center" prop="arriveState" width="90">
      <template v-slot = "scope">
        <dict-tag :options="dict.type.purchase_arrive" :value="scope.row.arriveState"/>
      </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="btsState"width="90" >
        <template v-slot = "scope">
          <dict-tag :options="dict.type.purchase_bts_state" :value="scope.row.arriveState"/>
        </template>
      </el-table-column>
      <el-table-column label="采购订单编号" align="center" prop="sn" />
<!--      <el-table-column label="采购名称" align="center" prop="name" />-->
<!--      <el-table-column label="采购员" align="center" prop="userId" />-->
<!--      采购员即创建者-->
      <el-table-column label="采购员" align="center" prop="userName" />
      <el-table-column label="供应商" align="center" prop="supplierName" />
      <el-table-column label="创建者" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleUpdate(scope.row,scope.$index)"
            v-hasPermi="['mes:order:edit']"
          >到货确认</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购订单编号:" prop="sn" label-width="55">
          <span v-if="orderDetailsList && orderDetailsList.length > 0">{{ orderDetailsList[0].sn }}</span>
          <span v-else>采购订单编号未提供</span>
        </el-form-item>
        <el-divider>物料列表</el-divider><!--分割线-->
<!--        <el-form-item label="采购名称" prop="name">-->
<!--          <el-input v-model="form.name" placeholder="请输入采购名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="到货状态" prop="arriveState">-->
<!--          <el-input v-model="form.arriveState" placeholder="请输入到货状态" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
        <el-card shadow="always" style="border-radius: 15px;">
          <el-table style="border-radius: 15px;" border v-loading="loading" :data=" orderDetailsList">
            <el-table-column label="物料名称" prop="matName" align="center">
            </el-table-column>
            <el-table-column label="物料编码" prop="matSn" align="center" width="150">
            </el-table-column>
            <el-table-column label="供应商" prop="supplierName" align="center">
            </el-table-column>
            <el-table-column label="到货数量" prop="reqNum" align="center">
            </el-table-column>
            <el-table-column label="价格" prop="matPrice" align="center">
              <template slot-scope="scope">
                <span>{{scope.row.matPrice * scope.row.reqNum}}</span>
              </template>
            </el-table-column>
            <el-table-column label="到货时间" prop="bpsDate" align="center" width="180">
              <template slot-scope="scope">
                <el-date-picker v-model="scope.row.bpsDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择到货日期"
                                style="width: 100%;">
                </el-date-picker>
              </template>
            </el-table-column>
<!--            目前仅考虑批量到货-->
<!--            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--              <template slot-scope="scope">-->
<!--                <el-button  size="mini" type="text" icon="el-icon-check">到货</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
          </el-table>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrder, getOrder, delOrder, addOrder, updateOrder, getOrderDetails,updateOrderAddPut} from "@/api/mes/order";
import Vue from "vue";

export default {
  name: "Order",
  dicts:["purchase_arrive","purchase_bts_state"],
  data() {
    return {
      // 遮罩层
      Index:0,
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购订单表格数据
      orderList: [],
      //采购订单详细信息列表
      orderDetailsList:[],
      //采购入库列表
      // storagePutList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        userId: null,
        btsState: null,
        createdBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.calculateIndex()
      });
    },
    /** 计算列表编号 */
    calculateIndex() {
      this.orderList.forEach((item, index) => {
        this.$set(item, 'number', index + 1);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sn: null,
        name: null,
        userId: null,
        arriveState: null,
        btsState: null,
        planId: null,
        isDeleted: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row,index) {
      this.reset();
      const id = row.id || this.ids
      const proId = row.proId
      getOrder(id).then(response => {
        this.form = response.data;
        this.Index = index;
        getOrderDetails(proId).then(response => {
          this.orderDetailsList = response;
        });
        this.open = true;
        this.title = "到货确认";
      });
    },
    submitForm() {
      // 创建一个空数组来存储新的入库信息
      this.storagePutList = this.orderDetailsList.map(item => ({
        purchaseSn: item.sn,
        matId: item.matId,
        bomId: item.bomId,
        bpsDate: item.bpsDate,
        bpsNum: item.reqNum,
        proId:item.proId,
        supplierId:item.supplierId,
        orderId:item.orderId,
      }));
      updateOrderAddPut(this.storagePutList).then(response => {
        // 提示入库申请成功
        this.$modal.msgSuccess("申请入库成功");
        // 清空 storagePutList 数组
        this.storagePutList = [];
        this.open = false;
        this.getList();
      }).catch(error => {
        this.$modal.msgError("申请入库失败");
        console.error(error);
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采购订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
